package com.thienloc.omap.util;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import javax.faces.context.FacesContext;

/**
 * Factory class for retrieving server-side i18n messages for the Java ES(tm)
 * Monitoring Console within the JSF framework. This class does not override
 * all methods in com.sun.webui.jsf.util.MessageUtil; it merely provides a few
 * commonly used methods for getting resources using the Resources.properties
 * files for this web application.
 *
 * @version 1.3 10/10/05
 * @author  Sun Microsystems, Inc.
 */
public class MessageUtil {

    /** The ResourceBundle basename for this web app. */
    static final public String BASENAME = "org.aptech.eticket.resources.Bundle";

    static public ResourceBundle getBundle(String resource) {
        return ResourceBundle.getBundle(resource, getLocale());
    }

    static public ResourceBundle getBundle(String resource, Locale locale) {
        return ResourceBundle.getBundle(resource, locale);
    }

    /** Get a message from the application's resource file. */
    static public String getMessage(String key) {
        return getMessage(key, (Object[]) null);
    }

    /** Get a formatted message from the application's resource file. */
    static public String getMessage(String key, Object[] args) {
        ResourceBundle bundle = ResourceBundle.getBundle(BASENAME, getLocale());
        return getMessage(key, args, bundle);
    }

    /** Get a message from the application's resource file. */
    static public String getMessage(String key, Locale locale) {
        return getMessage(key, (Object[]) null, locale);
    }

    /** Get a formatted message from the application's resource file. */
    static public String getMessage(String key, Object[] args, Locale locale) {
        ResourceBundle bundle = ResourceBundle.getBundle(BASENAME, locale);
        return getMessage(key, args, bundle);
    }

    /** Get a message from the given resource file. */
    static public String getMessage(String key, ResourceBundle bundle) {
        return getMessage(key, (Object[]) null, bundle);
    }

    /** Get a formatted message from the given resource file. */
    static public String getMessage(String key, Object[] args, ResourceBundle bundle) {

        if (key == null) {
            return key;//return null;
        }

        if (bundle == null) {
            throw new NullPointerException("Could not obtain resource bundle");
        }

        String message = null;
        try {
            message = bundle.getString(key);
        } catch (MissingResourceException e) {
        }

        return getFormattedMessage((message != null) ? message : key, args);
    }

    /**
     * Format message using given arguments.
     *
     * @param message The string used as a pattern for inserting arguments.
     * @param args The arguments to be inserted into the string.
     */
    static protected String getFormattedMessage(String message, Object args[]) {
        if ((args == null) || (args.length == 0)) {
            return message;
        }

        String result = null;
        try {
            MessageFormat mf = new MessageFormat(message);
            result = mf.format(args);
        } catch (NullPointerException e) {
        }

        return (result != null) ? result : message;
    }

    /** Get locale from the FacesContext object. */
    protected static Locale getLocale() {
        FacesContext context = FacesContext.getCurrentInstance();
        if (context == null) {
            return Locale.getDefault();
        }

        // context.getViewRoot() may not have been initialized at this point.
        Locale locale = null;
        if (context.getViewRoot() != null) {
            locale = context.getViewRoot().getLocale();
        }

        return (locale != null) ? locale : Locale.getDefault();
    }
} // MessageUtil

